from none_bot_py_plugins.gen_svr.report_svr import *


class ReportManagerGen(GenSvrBase):

    def __init__(self, recv_id):
        super(ReportManagerGen, self).__init__(recv_id)
        self.__run_map = {}
        self.__cast_map = {
            "create": lambda msg, state: self.create_svr(msg, state)
        }
        self.__call_map = {
            "create": lambda msg, state: self.create_svr_call(msg, state)
        }

    def handle_cast(self, from_id, msg, state):
        match, args = msg
        if match in self.__cast_map:
            return self.__cast_map[match](args, state)
        return "ok", state

    def handle_info(self, msg, state):
        return "ok", state

    def handle_call(self, from_id, msg, state):
        match, args = msg
        if match in self.__call_map:
            return self.__call_map[match](args, state)
        return "ok", "false", state

    def handle_error(self, msg, state):
        pass

    def handle_stop(self, result, state):
        pass

    def _init_(self, *args):
        print("report gen start")
        return []

    def create_svr(self, svr_name, state):
        if svr_name in self.__run_map:
            return "ok", state
        name = report_svr_name(svr_name)
        self.__run_map[name] = ReportSvr(name)
        self.__run_map[name].task_start()
        return "ok", state

    def create_svr_call(self, svr_name, state):
        result, new_state = self.create_svr(svr_name, state)
        return result, svr_name, new_state


def report_svr_name(name: str):
    return "report_" + name


def manager_start():
    report_gen_name = "report_gen"
    report_gen = ReportManagerGen(report_gen_name)
    report_gen.task_start([])
    return report_gen_name, report_gen
